home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Text / print / HPDJ900Src.lha / render.c < prev    next >
C/C++ Source or Header  |  2004-05-21  |  12KB  |  334 lines

  1. /*
  2.  *        Render function
  3.  *
  4.  *        HP_DeskJet_900C driver with color gfx
  5.  */
  6.  
  7. #include "global.h"
  8.  
  9. #define NUMSTARTCMD     7       /* # of cmd bytes before binary data */
  10. #define NUMENDCMD       0       /* # of cmd bytes after binary data */
  11. #define NUMTOTALCMD (NUMSTARTCMD + NUMENDCMD)   /* total of above */
  12. #define LENGTH          8
  13. #define PLANES         27
  14. #define WIDTH          33
  15. #define NUMCOLORCMD     7       /* Cmds for each color buffer */
  16. #define MAXCOLORBUFS    4
  17. #define STARTCMDSIZE   48
  18.  
  19. /*
  20.         00-04   \033&l0L        perf skip mode off
  21.         05-11   \033&l000F      set page length (LENGTH)
  22.         12-18   \033*t075R      set raster graphics resolution (dpi=15)
  23.         19-23   \033*b2M        add compression (Method 2)
  24.         24-29   \033*r01U       Num of raster planes per row (PLANES)
  25.         30-37   \033*r1440S     set raster gfx width (WIDTH)
  26.         38-42   \033*o3D        Set Gfx depletion (mid value)
  27.         43-47   \033*r0A        start raster graphics
  28. */
  29. char StartCmd[STARTCMDSIZE+1] = "\033&l0L\033&l000F\033*t075R\033*b0M\033*r01U\033*r1440S\033*o3D\033*r0A";
  30. static UWORD RowSize, NumColorBufs;
  31. STATIC UBYTE *gamma_table;
  32.  
  33. extern struct PrinterData *PD;
  34. extern struct PrinterExtendedData *PED;
  35. extern UBYTE GammaTables[15][256];
  36.  
  37. /* Render graphics to printer */
  38.  
  39. int Render(long ct, long x, long y, long status)
  40. {
  41.  
  42.         static ULONG BufSize;
  43.         static UWORD textlength;
  44.         static UBYTE *colors[MAXCOLORBUFS];
  45.         UBYTE *ptr;
  46.         int i, err, papersize, lpi, len;
  47.         long row;
  48.  
  49.         err=PDERR_NOERR;
  50.         switch(status) {
  51.                 case 0 : /* Master Initialization */
  52.                         /*
  53.                                 ct      - pointer to IODRPReq structure.
  54.                                 x       - width of printed picture in pixels.
  55.                                 y       - height of printed picture in pixels.
  56.                         */
  57.                         RowSize = (x + 7) / 8;
  58.                        if (PD->pd_Preferences.PrintShade == SHADE_COLOR) {
  59.                            NumColorBufs = MAXCOLORBUFS;
  60.                            StartCmd[PLANES] = '-'; /* 4 planes, KCMY */
  61.                            StartCmd[PLANES+1] = '4';
  62.                         }
  63.                         else
  64.                         {
  65.                            NumColorBufs = 1;
  66.                         }
  67.                         BufSize = RowSize * 2; /* Double buffered */
  68.  
  69.                         lpi = 6;
  70.                         if (PD->pd_Preferences.PrintSpacing == EIGHT_LPI)
  71.                            lpi = 8;
  72.  
  73.                         papersize = PD->pd_Preferences.PaperSize;
  74.                 switch (papersize)
  75.                 {
  76.                 case US_LETTER:
  77.                               textlength = 10 * lpi;
  78.                     break;
  79.                 case US_LEGAL:
  80.                               textlength = 13 * lpi;
  81.                                 break;
  82.                 case EURO_A4:
  83.                                textlength = (lpi ==8 ? 85 : 65);
  84.                     break;
  85.                 default:   /* Default size */
  86.                               textlength = PD->pd_Preferences.PaperLength;
  87.                 }
  88.  
  89.                         /* Set page length */
  90.                         StartCmd[LENGTH] = textlength / 100 | '0';
  91.                         textlength = textlength % 100;
  92.                         StartCmd[LENGTH+1] = textlength / 10 | '0';
  93.                         StartCmd[LENGTH+2] = textlength % 10 | '0';
  94.  
  95.                         row = x;           /* Set Raster Width */
  96.                         StartCmd[WIDTH] = row / 1000 | '0';
  97.                         row = row % 1000;
  98.                         StartCmd[WIDTH+1] = row / 100 | '0';
  99.                         row = row % 100;
  100.                         StartCmd[WIDTH+2] = row / 10 | '0';
  101.                         StartCmd[WIDTH+3] = row % 10 | '0';
  102.  
  103.                         if (PD->pd_Preferences.PrintThreshold>0)
  104.                            gamma_table = GammaTables[PD->pd_Preferences.PrintThreshold];
  105.                         else
  106.                            gamma_table = NULL;
  107.  
  108.  
  109.                         for (i=0; i< NumColorBufs; i++) {
  110.                             if (!(colors[i] = AllocMem(BufSize, MEMF_PUBLIC)))
  111.                                err = PDERR_BUFFERMEMORY;
  112.                         }
  113.  
  114.                         if (err != PDERR_BUFFERMEMORY) {
  115.                             /* perf skip mode off, set dpi, start raster gfx */
  116.                             err = (*(PD->pd_PWrite))(StartCmd, STARTCMDSIZE-1);
  117.                         }
  118.                         break;
  119.  
  120.                 case 1 : /* Scale, Dither and Render */
  121.                         /*
  122.                                 ct      - pointer to PrtInfo structure.
  123.                                 x       - 0.
  124.                                 y       - row # (0 to Height - 1).
  125.                         */
  126.                         Transfer((struct PrtInfo *)ct, y,colors, RowSize, NumColorBufs);
  127.                         err = PDERR_NOERR; /* all ok */
  128.                         break;
  129.  
  130.                 case 2 : /* Dump Buffer to Printer */
  131.                         /*
  132.                                 ct      - 0.
  133.                                 x       - 0.
  134.                                 y       - # of rows sent (1 to NumRows).
  135.  
  136.                                 White-space strip.
  137.                         */
  138.  
  139.                        if (gamma_table != NULL && NumColorBufs == 4)
  140.                           CorrectColours(gamma_table,colors, BufSize);
  141.  
  142.                        if (NumColorBufs == 4)
  143.                           err = DumpColorPrint(colors, BufSize);
  144.                        else
  145.                           err = DumpBWPrint(colors,BufSize);
  146.                        break;
  147.  
  148.                 case 3 : /* Clear and Init Buffer */
  149.                         /*
  150.                                 ct      - 0.
  151.                                 x       - 0.
  152.                                 y       - 0.
  153.                         */
  154.                         for (i=0; i<NumColorBufs; i++) {
  155.                             ptr = colors[i];
  156.                             len = BufSize;
  157.                             do {
  158.                                 *ptr++ = 0;
  159.                             } while (--len);
  160.                         }
  161.                         break;
  162.  
  163.                 case 4 : /* Close Down */
  164.                         /*
  165.                                 ct      - error code.
  166.                                 x       - io_Special flag from IODRPReq struct
  167.                                 y       - 0.
  168.                         */
  169.                         err = PDERR_NOERR; /* assume all ok */
  170.                         /* if user did not cancel the print */
  171.                         if (ct != PDERR_CANCEL) {
  172.                                 /* end raster graphics, perf skip mode on */
  173.                                 if ((err = (*(PD->pd_PWrite))
  174.                                         ("\033*rbC\033&l1L", 10)) == PDERR_NOERR) {
  175.                                         /* if want to unload paper */
  176.                                         if (!(x & SPECIAL_NOFORMFEED)) {
  177.                                                 /* eject paper */
  178.                                                 err = (*(PD->pd_PWrite))
  179.                                                         ("\014", 1);
  180.                                         }
  181.                                 }
  182.                         }
  183.                         /*
  184.                                 flag that there is no alpha data waiting that
  185.                                 needs a formfeed (since we just did one)
  186.                         */
  187.                         PED->ped_PrintMode = 0;
  188.                          /* wait for both buffers to empty */
  189.                         (*(PD->pd_PBothReady))();
  190.                         for (i=0; i<NumColorBufs; i++) {
  191.                            if (colors[i] != NULL)
  192.                                 FreeMem(colors[i], BufSize);
  193.                         }
  194.                         break;
  195.  
  196.                 case 5 : /* Pre-Master Initialization */
  197.                         /*
  198.                                 ct      - 0 or pointer to IODRPReq structure.
  199.                                 x       - io_Special flag from IODRPReq struct
  200.                                 y       - 0.
  201.                         */
  202.                         /* select density */
  203.                         SetDensity(x & SPECIAL_DENSITYMASK);
  204.                         break;
  205.         }
  206.         return(err);
  207. }
  208.  
  209. /* New dump routines which replaces older CompactBuf routine 8/4/02 */
  210.  
  211. int DumpBWPrint(UBYTE *colors[],ULONG BufSize)
  212. {
  213.     /* Dump B&W grpahic data */
  214.  
  215.     ULONG size;
  216.     UWORD i=0;
  217.     UBYTE *ptr;
  218.     char cmd[10] = "\033*b0m000W";
  219.     int err, method, mem;
  220.  
  221.     mem = FALSE;
  222.     /* Remove trailing white space */
  223.     size = StripWhiteSpace(colors[0], BufSize);
  224.     if (size>0) {
  225.         /* Compress data */
  226.         if (ptr = AllocMem(BufSize, MEMF_PUBLIC|MEMF_CLEAR)) {
  227.             i = CompressMethod2(colors[0], ptr, size);
  228.             mem = TRUE;
  229.             if (i<=0) {
  230.                 method = 0;
  231.                 i = size;
  232.             } else
  233.                 method = 2;
  234.         }
  235.         else {
  236.             mem = FALSE;
  237.             ptr = colors[0];
  238.             method = 0;
  239.         }
  240.  
  241.         cmd[3] = method | '0';
  242.         cmd[5] = (i/100) | '0';
  243.         cmd[6] = (i - (i/100)*100) / 10 | '0';
  244.         cmd[7] = i % 10 | '0';
  245.     } else {
  246.         method = 0;
  247.         i = 0;
  248.         cmd[3] = method | '0';
  249.         cmd[5] = '0';
  250.         cmd[6] = '0';
  251.         cmd[7] = '0';
  252.         ptr=colors[0];
  253.     }
  254.  
  255.     (*(PD->pd_PWrite))(cmd,9);
  256.     err = (*(PD->pd_PWrite))(ptr, i);
  257.     if (mem) FreeMem(ptr, BufSize); /* Free memory if allocated */
  258.     return err;
  259. }
  260.  
  261. int DumpColorPrint(UBYTE *colors[],ULONG BufSize)
  262. {
  263.     /* Dump Color graphic data */
  264.  
  265.     ULONG size;
  266.     UWORD i=0;
  267.     UBYTE *ptr;
  268.     char startcmd[6] = "\033*b0W";
  269.     char colorcmd[10] = "\033*b0m000V";
  270.     int err, ct, method, mem;
  271.  
  272.     mem = FALSE;
  273.     err = (*(PD->pd_PWrite))(startcmd,5);
  274.  
  275.     for (ct=0; ct<4 && err == PDERR_NOERR; ct++) {
  276.  
  277.         /* Strip trailing White Space */
  278.         size = StripWhiteSpace(colors[ct], BufSize);
  279.         if (size>0) {
  280.             /* Compress data */
  281.             if (ptr = AllocMem(BufSize, MEMF_PUBLIC|MEMF_CLEAR)) {
  282.                  i = CompressMethod2(colors[ct], ptr, size);
  283.                  mem = TRUE;
  284.                  if (i <= 0) {
  285.                     method = 0;
  286.                     i = size;
  287.                  } else
  288.                     method = 2;
  289.             }
  290.             else {
  291.                  mem = FALSE;
  292.                  ptr = colors[ct];
  293.                  method = 0;
  294.             }
  295.  
  296.             colorcmd[3] = method | '0';
  297.             colorcmd[5] = (i/100) | '0';
  298.             colorcmd[6] = (i - (i/100)*100) / 10 | '0';
  299.             colorcmd[7] = i % 10 | '0';
  300.         } else {
  301.             method = 0;
  302.             i = 0;
  303.             colorcmd[3] = method | '0';
  304.             colorcmd[5] = '0';
  305.             colorcmd[6] = '0';
  306.             colorcmd[7] = '0';
  307.             ptr = colors[ct];
  308.         }
  309.         (*(PD->pd_PWrite))(colorcmd,9);
  310.         err = (*(PD->pd_PWrite))(ptr, i);
  311.         if (mem) FreeMem(ptr, BufSize);
  312.     }
  313.    return err;
  314. }
  315.  
  316.  
  317. /* This routine makes a local copy of the colour data and applies
  318.  * gamma correction to it.
  319.  */
  320. VOID CorrectColours(UBYTE *gamma_table, UBYTE *colors[], ULONG width)
  321. {
  322.     LONG x,c;
  323.         UBYTE *ptrstart;
  324.  
  325.         for (c=0; c<=3; c++) {  /* Repeat for CMYK colours */
  326.            ptrstart = colors[c];
  327.  
  328.        for(x = 0 ; x < width ; x++) /* for each byte */
  329.        {
  330.         *(ptrstart++) = gamma_table[*ptrstart];
  331.            }
  332.         }
  333. }
  334.